import Taro, { getCurrentInstance } from '@tarojs/taro';
import React, { Component } from 'react';
import { cache } from 'config';
import * as T from './types';
import { WMkit, msg, pvUvStatics } from 'wmkit';
// import Live from '@/pages/live';
import GoodsAllList from './all-list';

//@ts-ignore
export default class ClassifyCircleLoadPage extends Component<Partial<T.IProps>, any> {
  constructor(props) {
    super(props);
    this.state = {
      init: true,
    };
  }

  componentWillMount() {
    if (__TARO_ENV !== 'h5') {
      Taro.showShareMenu({
        withShareTicket: true,
      });
    }
  }
  onShareAppMessage() {
    let wechatShareInfo = JSON.parse(Taro.getStorageSync('wechatShareInfo'));
    return {
      title: wechatShareInfo.title,
      imageUrl: wechatShareInfo.imgUrl[0].url,
    };
  }
  onShareTimeline() {
    // 默认分享内容
  }

  //为了使每次显示的时候都能刷新
  async componentDidShow() {
    //埋点
    pvUvStatics.myPvUvStatis({});

    if (WMkit.needLogin()) {
      Taro.redirectTo({
        url: '/pages/package-C/login/login/index',
      });
      return;
    }
    //避免h5一直刷新
    //取缓存中的是否开启分销
    //查询分销是否开启，并存入缓存
    //如果状态不同，更新tabbar
    // const preIsOpen = Taro.getStorageSync(cache.IS_OPEN_DISTRIBUTOR);
    // const isOpen = await WMkit.isOpenDistributor();

    // //对比分销员资格
    // let isChangedistributorFlag = false;
    // if (WMkit.isLogin()) {
    //   const preDistributorFlag = Taro.getStorageSync(cache.DISTRIBUTOR_FLAG);
    //   await WMkit.setIsDistributorFlag();
    //   const distributorFlag = Taro.getStorageSync(cache.DISTRIBUTOR_FLAG);
    //   isChangedistributorFlag = preDistributorFlag != distributorFlag;
    // }

    // // 是否开启直播
    // const preIsOpenLive = Taro.getStorageSync(cache.IS_OPEN_LIVE);
    // const isOpenLive = await WMkit.isLiveOpen();

    // if (preIsOpen != isOpen || isChangedistributorFlag || preIsOpenLive != isOpenLive) {
    //   WMkit.changeTabBarText();
    // }
    // const current = getCurrentInstance();
    // const url = __TARO_ENV === 'h5' ? current?.page?.path : current?.router?.path;
    // if (url.indexOf('/pages/package-S/classify-circle-load-page/index') > -1) {
    //   //更新标题
    //   await Taro.setNavigationBarTitle({
    //     title:
    //       Taro.getStorageSync(cache.IS_OPEN_DISTRIBUTOR) && Taro.getStorageSync(cache.IS_OPEN_LIVE) ? '发现' : '分类',
    //   });
    // }

    //强制子组件重新渲染
    this.setState({
      init: true,
    });
    msg.emit('start-circular');
  }

  async componentDidMount() { }

  componentWillUnmount() { }

  componentDidHide() {
    msg.emit('stop-circular');
  }

  render() {
    const token = Taro.getStorageSync('authInfo:token');

    // return Taro.getStorageSync(cache.IS_OPEN_DISTRIBUTOR) &&
    //   Taro.getStorageSync(cache.IS_OPEN_LIVE) &&
    //   this.state.init ? (
    //   <Live />
    // ) : (
    //   <GoodsAllList token={token} />
    // );

    return <GoodsAllList token={token} />
  }
}

//create by moon https://github.com/creasy2010/moon
